<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class HotnewController extends CommonController {

	public function index() {

		$M=new \Think\Model("shangpin");
		$count = $M->count();
  
		$Page =new \Think\Page($count,20);
		$Page -> setConfig('first','首页');
		$Page -> setConfig('last','末页');
		$Page -> setConfig('prev','上一页');
		$Page -> setConfig('next','下一页');
		$Page -> setConfig('theme',' <div style="margin-top:2px">  %FIRST%  %UP_PAGE%    %LINK_PAGE%  %DOWN_PAGE%   %END% </div> ');



		$show = $Page->show();

	
        $list=M('shangpin')->order("id desc")->limit($Page->firstRow, $Page->listRows)->select();
        // $strsql = "select a.id,a.name,a.fengge,a.huxing,a.area,a.price,a.intro,a.img,a.userid,b.user as username ";
        // $strsql .="from zyb_shangpin a,zyb_user  b where a.userid = b.id and a.userid = $userid order by a.id desc ";

		foreach ($list as  &$value) {
			if (strlen($value['name'])>15) {
			$value['name']=mb_substr($value['name'],0,8,'utf-8').'...';

			}
         if(strlen($value['intro'])>30){
            $value['intro']=mb_substr(strip_tags(html_entity_decode($value['intro'])),0,30,'utf-8').'...';
         }else{$value['intro']=html_entity_decode($value['intro']);}

       
        // $type = M('zlist')->where(array('id'=>$value['type']))->find();

        // $list[$key]['type'] =$type['type_name'];
        
        }


        $this->assign("pageCount",$count);
		$this->assign("list", $list);
		$this->assign("page",$show);

		$this->display();
	}
	
	
	/**
	 +----------------------------------------------------------
	 * 父栏目
	 +----------------------------------------------------------
	 */
	 public function lider(){

	 	$lider = M('zlist');
	 	$list = $lider->select();
	 	$this->assign('list',$list);


	 	$this->display();
	 }

     public function huxing(){

	 	$huxing = M('huxing');
        $count = M('huxing')->count();
  
		$Page =new \Think\Page($count,20);
		$Page -> setConfig('first','首页');
		$Page -> setConfig('last','末页');
		$Page -> setConfig('prev','上一页');
		$Page -> setConfig('next','下一页');
		$Page -> setConfig('theme',' <div style="margin-top:2px">  %FIRST%  %UP_PAGE%    %LINK_PAGE%  %DOWN_PAGE%   %END% </div> ');



		$show = $Page->show();


	 	$list = $huxing->order('id desc')->limit($Page->firstRow, $Page->listRows)->select();
	 	$this->assign('huxing',$list);
	$this->assign("page",$show);

	 	$this->display();
	 }


	 public function delete(){
	 	$id = I('get.id');
	 	if ($id>0) {
	 		M('zlist')->where(array('id'=>$id))->delete();
                   
	 		//M('zixun')->where(array('type'=>$id))->delete();

	 		$this->success('删除成功',U('Hotnew/lider'));
	 		exit();
	 		# code...
	 	}


	 }

	 public function huxingdel(){
	 	$id = I('get.id');
	 	if ($id>0) {
	 		M('huxing')->where(array('id'=>$id))->delete();
                   
	 		//M('zixun')->where(array('type'=>$id))->delete();

	 		$this->redirect('Hotnew/huxing');
	 		exit();
	 		# code...
	 	}


	 }



		
	/**
	 +----------------------------------------------------------
	 * 添加管理员
	 +----------------------------------------------------------
	 */
    Public function addlist(){
    	if(IS_POST){
                    
      
    		$data=array();
    		$data=I('post.');  
             $result=M('huxing')->where('huxing="'.trim($data['huxing']).'"')->find();
             if($result){
             	$this->error('金重已存在',U('Hotnew/huxing'));
             }else{

    		if(M('huxing')->add($data)){
 				$this->redirect('Hotnew/huxing');
 				exit();
    		}else{
    			$this->success('添加失败',U('Hotnew/huxing'));
    			exit();
    		}

}

    	}




    	$this->display();
    }





    Public function addhuxing(){
    	if(IS_POST){
                    

    		$data=array();
    		$data=I('post.');



    		if(M('huxing')->add($data)){
 				$this->success('分类添加成功',U('Hotnew/index'));
 				exit();
    		}else{
    			$this->success('分类添加失败',U('Hotnew/index'));
    			exit();
    		}



    	}




    	$this->display();
    }



		
	/**
	 +----------------------------------------------------------
	 * 添加管理员
	 +----------------------------------------------------------
	 */



	public function addAdmin() {
	
		if (IS_POST) {
			$data=array();
			$data=I('post.');
			$data['jinz']=implode(',', $data['jinz']);
			$data['yanse']=implode(',', $data['yanse']);
	       $data['addtime']=time();
          $data['intro']=htmlspecialchars_decode(I('post.intro'));
			if(M('shangpin')->add($data)){
				// $this->success('添加成功',U('Hotnew/index'));
				$this->redirect('Hotnew/index');
			}else{
				$this->error('添加失败',U('Hotnew/index'));
			}
		} else {
            	$lis=M('zlist');
		$list=$lis->select();
		$ljj=M('huxing')->select();
        $kuanshi=M('kuanshi')->select();
        $this->assign('kuanshi',$kuanshi);
		$this->assign('ljj',$ljj);
		$this->assign('list',$list);
		$this->assign("info", $this->getCatOption());
              

		
			$this->display();
		}
	}
	

	/**
	 +----------------------------------------------------------
	 * 修改密码
	 +----------------------------------------------------------
	 */
		public function edit() {

	        $id=I('get.id');
		    $zi=M('shangpin');
		    $map['id']=$id;

		    $info1=$zi->where($map)->find();
		  
		    $info1['intro'] =html_entity_decode($info1['intro']);

		    // $this->assign('info1',$info1);
	      $info1['jinz']=explode(',', $info1['jinz']);
	       $info1['yanse']=explode(',', $info1['yanse']);

			$ljj=M('huxing')->select();
		$this->assign('ljj',$ljj);
			$kuanshi=M('kuanshi')->select();
		$this->assign('kuanshi',$kuanshi);
			$this->assign("info", $this->getCatOption($info1));
          $zlist=M('zlist');
          $list=$zlist->select();
          $this->assign("list",$list);

      
		if (IS_POST) {

			  $data1['dd']=I('post.id');
			  	$data['cid']=I('post.cid');
			  		$data['kuanshi']=I('post.kuanshi');
			  		$data['img']=I('post.img');
			  	$data['name']=I('post.name');
			$data['chengse']=I('post.chengse');
		    $data2['jinz']=I('post.jinz');
		     $data2['yanse']=I('post.yanse');
		    $data['jinz']=implode(',', $data2['jinz']);
			$data['yanse']=implode(',', $data2['yanse']);
				$data['guige']=I('post.guige');
            if(empty($data['img'])){

            	 unset($data['img']);
            }
     
           $data['intro']=htmlspecialchars_decode(I('post.intro'));
   
	   
			if(M('shangpin')->where("id=".$data1['dd'])->save($data)){
				$this->redirect('Hotnew/index');
				//$this->success('修改成功',U('Hotnew/index'));
			}else{
				$this->success('未修改',U('Hotnew/index'));
			}
		} else {
		
			$this->display();
		}
	}
	
	
	/**
	 +----------------------------------------------------------
	 * 修改 管理员
	 +----------------------------------------------------------
	 */



	public function del(){

		$ids = $_POST['id'];


      
		$str="('".implode("','",$ids)."')";
	
      

	
		if (M("shangpin")->where("id in ".$str)->delete()) {
				$this->redirect('Hotnew/index');
			// $this->success('删除成功');
		} else {
			$this->success('删除失败');
		}
	}

public function fenlei() {

	        $id=I('get.id');
		    $zi=M('huxing');
		    $map['id']=$id;

		    $info=$zi->where($map)->find();
		  
		


		    $this->assign('info',$info);
	
  
      
		if (IS_POST) {
           
			$data=I('post.');

            $id=I('get.id');

			if(M('huxing')->where("id=".$data['dd'])->save($data)){
				$this->redirect('Hotnew/huxing');
			}else{
				$this->success('修改失败',U('Hotnew/huxing'));
			}
		} else {
		
			$this->display();

		}
	}
	

    public function Category() {
    	$M=M("category");
    	$count = $M->count();
        $page =new \Think\Page($count,10);
    	$showPage = $page->show();
    //	$this->assign("page", $showPage);
    	$this->assign("pageCount",$count);
    	$this->assign("list", D('Products')->catList());
    	$this->display('category_list');
    }

 //分类添加
    public function addCategory() {
    	$M = D("Products");
    	if(IS_POST){
    		header('Content-Type:application/json; charset=utf-8');
    		echo json_encode($M->addCategory());
    	}else{

    		$this->assign("info",$this->getCatOption());
    		$this->display("category_add");
    	}
    }
    
    private function getCatOption($info = array()) {
    	//import("@.ORG.Category");
    	$cat = new \Admin\ORG\Category('Category', array('cid', 'pid', 'name', 'fullname'));

    	$list = $cat->getList();               //获取分类结构
    	foreach ($list as $k => $v) {
    		$disabled = $v['pid'] == ($info['id']?'':$info['cid'])||$v['cid'] == ($info['id']?'':$info['cid']) ? ' disabled="disabled"' : "";
    		$selected = $v['cid'] == ($info['id'] ?$info['cid']:$info['pid'])? ' selected="selected"' : "";
    		$info['pidOption'].='<option value="' . $v['cid'] . '"' . $selected . $disabled . '>' . $v['fullname'] . '</option>';
    	}
    	return $info;
    }


    public function editCategory() {
    	$M = M("Category");
    	if(IS_POST){
    		header('Content-Type:application/json; charset=utf-8');
    		echo json_encode(D("Products")->editCategory());
    	}else{
    		$list=M("Category")->where("pid=0")->order("pid desc")->select();
    		$this->assign("list",$list);
    		$info = $M->where("cid=" . (int) $_GET['cid'])->find();
    		$this->assign("info", $this->getCatOption($info));
    		$this->display("category_add");
    	}
    }




    //分类删除
    public function delCategory() {
    	$ids = is_numeric($_GET['cid']) ? array($_GET['cid']) : $_POST['cid'];
    	$str="('".implode("','",$ids)."')";
    	if (M("Category")->where("cid in ".$str)->delete()) {
    		$this->redirect('Hotnew/Category');
    	} else {
    		$this->error('删除失败，可能是不存在该ID的记录');
    	}
    }


    
    public function tupian(){


       


 if (!empty($_FILES)) {

            //图片上传设置
             $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     553145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
                $upload->savePath  =     ''; // 设置附件上传（子）目录
                // 上传文件
       
            $images = $upload->upload();
    
            //判断是否有图
            if($images){
                $info=$images['Filedata']['savepath'].$images['Filedata']['savename'];
  


                //返回文件地址和名给JS作回调用
                echo $info;
         
                 
            }
            else{
                $this->error($upload->getError());//获取失败信息
            }
        }else{

        }







    }




    		
}

    
  


?>